//
//  LDYvisitorView.swift
//  李东阳微博
//
//  Created by 李东阳 on 16/4/16.
//  Copyright © 2016年 李东阳. All rights reserved.
//

import UIKit
import SnapKit
class LDYvisitorView: UIView {

        //MARK: -  定义一个闭包
    var loginClosure:(() -> ())?
    
   override  init(frame: CGRect) {
    
    super.init(frame: frame)
    
    setupUI()
    
    
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }

        //MARK: -   外界调用方法
    func setVisitorInfo(ImaName:String?, title:String?){
    
        if let img = ImaName, tit = title{
        
            iconImageView.image = UIImage(named: img)
            iconLabel.text = tit
            circleImageView.hidden = true
        
        }else{
        
              setCircleAnim()
        
        }
        
   
        
    }

    
    
        //MARK: -  监听事件
    @objc private func loginClick(){
    
       loginClosure?()
    print("按钮被点击了")
    }
    
    
    //圆圈转起来
    private func setCircleAnim(){
    
        
    let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置动画执行时间
         anim.duration = 20
        
        //设置tovalue
        anim.toValue = 2 * M_PI
        
        //执行次数
        anim.repeatCount = MAXFLOAT

        anim.removedOnCompletion = false
        
       circleImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    
        //MARK: -  设置视图颜色
    private func setupUI(){
        
    
//    self.backgroundColor = randomColor()
        
        
            //MARK: -  添加控件
    addSubview(circleImageView)
    addSubview(maskImageView)
    addSubview(iconImageView)
    addSubview(iconLabel)
    addSubview(registerButton)
    addSubview(loginButton)
   
            //MARK: -  添加约束
        circleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
        
        make.center.equalTo(self)
        }
      
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
       
        
        iconLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            make.width.equalTo(224)
            make.height.equalTo(36)
        
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconLabel)
            make.top.equalTo(iconLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))

        }
        
    loginButton.snp_makeConstraints { (make) -> Void in
        
         make.right.equalTo(iconLabel)
         make.top.equalTo(iconLabel.snp_bottom).offset(16)
         make.size.equalTo(CGSize(width: 100, height: 36))

        }
        
        
            //MARK: -  当我们切换tabbar 或者程序退到后台 默认移除动画
        

      
    }
    

        //MARK: -  懒加载控件circleImageView

    private lazy var circleImageView :UIImageView = UIImageView(image: UIImage (named: "visitordiscover_feed_image_smallicon"))
    
    //遮挡
    private lazy var maskImageView : UIImageView = {
    
    let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return img
    
    }()
    
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var iconLabel: UILabel = {
    
    let lab = UILabel(textColor: UIColor.grayColor(), textFont: 14)
     
      lab.text = "关注一些人，回这里看看有什么惊喜 关注一些人，回这里看看有什么惊喜"
        
         lab.numberOfLines = 0
        lab.textAlignment = .Center
        
        return lab
      
    }()
   
        //MARK: -   注册 登录按钮
    
    private lazy var registerButton : UIButton = {
        
        let button = UIButton()
    
       button.setBackgroundImage(UIImage(named: "common_button_white_disable") , forState: UIControlState.Normal)
        
            //MARK: -  添加title
        button.setTitle("注册", forState: UIControlState.Normal)
        
            //MARK: -  按钮的点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)

        
        button.setTitleColor(myTintColor, forState: UIControlState.Normal)
                button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)

       return button
        
        }()
    
        //MARK: -  登录按钮

    private lazy var loginButton : UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable") , forState: UIControlState.Normal)
        
            //MARK: -  添加title
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(myTintColor, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
        }()
    
    

    
}
